export default {
  name: 'vUpLoad',
  props: {
    option: {
      type: Object,
      default() {
        return {
          action: "http://127.0.0.1:7001/common/upLoad",
          limit: 1,
          maxSize: 150,
          data: { dir: 'active' },
        };
      },
    },
  },
  render(h) {
    return (
      <elUpload
        ref="upLoad"
        action={this.option.action}
        limit={this.option.limit}
        data={this.option.data}
        accept='image/*'
        list-type={this.option.listType || 'picture'}
        on-remove={this.remove}
        onExceed={this.exceed}
        before-upload={this.beforeUpload}
        onSuccess={this.upLoadOk}
      >
        {/* ${options.limit||1} */}
        <elButton size="small" type="primary">点击上传</elButton>
        <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,
          <span style="color:red" >最多张{this.xxx}</span> ,且不超过
          <b>{this.option.maxSize || 100}kb</b>
        </div>
      </elUpload>

    )
  },
  data() {
    return {
      xxx: 'sdfsdf'
    }
  },
  mounted() {
    console.log('图片上传了',this.$refs.upLoad)
  },
  methods: {
    upLoadOk(res, file, fileLis) {
      console.log('%c gc_res=>: ','color: MidnightBlue; background: Aquamarine; font-size: 20px;',res);

      let { response, uid } = file;
      if (response.code === 10200) {
        this.imgUrl[uid] = response.data.url;
        this.$emit('imgsChange', this.imgUrl);
        //
      } else {
        log.warn(response.msg);
        this.$refs.upLoad.clearFiles();
      }
    },
    exceed() {
      console.log('图片上传超过最大限制')
      log.warn('图片上传超过最大限制');
    },
    remove(file) {
      console.log('删除了')
      delete this.imgUrl[file.uid];
      this.$emit('imgsChange', this.imgUrl);
    },
    beforeUpload(file) {
      const { size } = file;
      if (size / 1000 > this.option.maxSize) {
        log.warn(`上传图片不能超过${this.option.maxSize}kb`);
        return false;
      }
    },
  }
}